$(document).ready(function () {
    /*查询试题类型*/
    $.ajax({
        type: "POST",
        url: "questionTypes",
        data: {},
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#typeId").html($("#typeId").html() + '<option value="' + data[i].questionTypeId + '">' + data[i].questionTypeName + '</option>')
            }
        }
    });

    /*查询科目*/
    $.ajax({
        type: "POST",
        url: "fields",
        data: {},
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#kindId").html($("#kindId").html() + '<option value="' + data[i].fieldId + '">' + data[i].fieldName + '</option>');
                $("#field").html($("#field").html() + '<option value="' + data[i].fieldId + '">' + data[i].fieldName + '</option>')
            }
        }
    });

    /*查询知识点*/
    $.ajax({
        type: "POST",
        url: "questionPoints",
        data: {},
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#pointId").html($("#pointId").html() + '<option value="' + data[i].pointId + '">' + data[i].pointName + '</option>');
                $("#point").html($("#point").html() + '<option value="' + data[i].pointId + '">' + data[i].pointName + '</option>')
            }
            /* if(data.length > 0){
             var str = "";
             jQuery.each(data, function (i, item) {
             str += "<option value=" + item.pointId + ">" + item.pointName + "</option>";
             $("#pointId").html("<option value=\"0\">知识点(全部)</option>" + str);
             });
             }*/
        }
    });
    search();
});

//回车键事件
var a = 1;
document.onkeydown = function (event) {
    if (a == 1) {
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if (e && e.keyCode == 13) {
            search();
        }
    }
};

//毫秒值格式化为时间格式
Date.prototype.toLocaleString = function () {
    return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate();
};

//搜索按钮
function search() {
    var creator1 = $("#creator").val();
    var typeId1 = $("#typeId").val();
    var kindId1 = $("#kindId").val();
    var pointId1 = $("#pointId").val();
    $.ajax({
        type: "POST",
        url: "List",
        data: {creator: creator1, typeId: typeId1, kindId: kindId1, pointId: pointId1},
        //   data: {creator: "", typeId: 0, kindId: 0, pointId: 0},
        dataType: "json",
        success: function (data) {
            var str = "";
            if (data.length > 0) {
                jQuery.each(data, function (i, item) {
                    var index = i + 1;
                    var unixTimestamp = new Date(item.questionCreateTime);
                    createTime = unixTimestamp.toLocaleString();
                    str += "<tr class=\"warning\">";
                    str += "<td>" + index + "</td>";
                    str += "<td><a href=" + "questionDetail/" + item.questionId + " style='color: #78b7de'>" + CutStr(item.questionName,item.questionName.length) + "</a></td>";
                    str += "<td>" + item.questionType.questionTypeName + "</td>";
                //    str += "<td>" + item.answer.answerOption + "</td>";
                //    str += "<td>" + item.questionAnswer + "</td>";
                    str += "<td>" + createTime + "</td>";
                    str += "<td>" + item.user.userName + "</td>";
                    //   str += "<td>" + item.questionCreator + "</td>";
                    str += "<td><a href=\"#myModal\" onclick='getid2(" + item.questionId + ")' data-toggle=\"modal\" class=\"btn btn-info\">编辑</a>";
                    str += "&nbsp;&nbsp;<a href=\"#myModal2\" onclick='getid(" + item.questionId + ")' data-toggle=\"modal\" class=\"btn btn-danger\">删除</a></td>";
                    str += "</tr>";
                    //   alert(str);
                    $("#tablist").html(str);
                });
            }
            else {
                $("#tablist").html("");
            }
            //执行分页函数,
            $(function () {
                //设置页数按钮所在的层,
                $("div.holder").jPages({
                    //显示数据所在的块的ID
                    containerID: "tablist",
                    first: '首页',
                    last: '尾页',
                    previous: '上页',
                    next: '下页',
                    //每页显示数据为多少行
                    perPage: 5,
                    //起始页
                    startPage: 1,
                    //开始页码为2个
                    startRange: 1,
                    //最多显示几个页码页码,其余用..代替
                    midRange: 4,
                    //结束页码为1个
                    endRange: 1,
                    //数据显示的动画效果
                    animation: 'wobble',
                    keyBrowse: true
                });
            });
        },
        error: function (XMLHttpRequest, textStatus) {
            //   alert(XMLHttpRequest.status);
            //    alert(XMLHttpRequest.readyState);
            //    alert(textStatus);
            layer.alert("数据读取错误!");
        }
    });
}

//获取当前用户的id,重新点击删除按钮时，错误信息隐藏
function getid(id) {
    //    alert(id);
    $("#userid").val(id);
}

function getid2(id) {
 //   alert(id);
    $("#userid2").val(id);
    loadedit();
}

//加载编辑框信息
function loadedit() {
    var id2 = $("#userid2").val();
    $.ajax({
        type: "POST",
        url: "loadEdit",
        data: {questionid: id2},
        dataType: "json",
        success: function (data) {
            console.log(data);
            //获取遮罩层三个下拉框的内容
            var sel = document.getElementById('field');
            var sel2 = document.getElementById('point');
            var sel3 = document.getElementById('answer');
            //循环判断，与后台数据比较，相等的话 selected
            for (var i = 0; i < sel.options.length; i++) {
                if (sel.options[i].value == data.fieldId) {
                    sel.options[i].selected = true;
                    break;
                }
            }
            for (var j = 0; j < sel2.options.length; j++) {
                if (sel2.options[j].value == data.pointId) {
                    sel2.options[j].selected = true;
                    break;
                }
            }
            /*for (var k = 0; k < sel3.options.length; k++) {
                if (sel3.options[k].value == data.difficultlevel) {
                    sel3.options[k].selected = true;
                    break;
                }
            }*/
        },
        error: function () {
            layer.alert("加载试题信息失败");
        }
    });
}


//编辑操作
function submit() {
    var id2 = $("#userid2").val();
    $.ajax({
        type: "POST",
        url: "questionInfo",
        data: {"questionId":id2,"fielsId":$("#field").val(),"pointId":$("#point").val(),"answer":$("#answer").val()},
        dataType: "json",
        success: function (data) {
            if(data === true){
                location.reload();
            }
            else {
                layer.alert("提交失败");
            }
        },
        error:function(){
            layer.alert("提交失败");
        }
    });
}


//删除操作
function del() {
    var id = $("#userid").val();
    //    alert(id);
    $.ajax({
        type: "POST",
        url: "delQuestion",
        data: {questionid: id},
        dataType: "json",
        success: function (data) {
            if (data === true) {
                //   layer.alert("操作成功");
                location.reload();
            }
            else {
                layer.alert("删除试题失败");
            }
        },
        error: function () {
            layer.alert("删除试题失败");
        }
    });
}

//试题长度20自动换行
function CutStr(strx, lenx) {
    var sx = "";
    if(lenx > 20){
        var lenx2 = Math.ceil(lenx/20);
        for(var i = 1; i <= lenx2; i++){
            sx += strx.substring((i-1)*20,i*20) + "<br>";
        }
        return sx;
    }
    else {
        sx = strx + "<br>";
        return sx;
    }
}